package camp.android.cookbook;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class RecipeListView extends ListActivity
{

	ArrayList<Recipe> recipes; 

	
	public static final String STORAGE_FILE = "recipe_list";
	
	public ArrayList<Recipe> getRecipesFromFile() { //return the ArrayList<Recipe> representation of our internal storage
		try{
			if (new File(getFilesDir(), STORAGE_FILE).exists())    {//if we already have some recipes, load them into the arraylist
				ObjectInputStream in = new ObjectInputStream(openFileInput(STORAGE_FILE));
				ArrayList<Recipe> toReturn = (ArrayList<Recipe>) in.readObject();
				in.close();
				return toReturn;
			}
			else {
				File f = new File(getFilesDir(), STORAGE_FILE);
				f.createNewFile();
				return new ArrayList<Recipe>();
			}
		} catch (IOException e) { //might want to individualize to IOException and ClassNotFoundException
			e.printStackTrace();
			System.out.println(e + " an IOException in getRecipesFromFile");
			return null; //this probably bad practice
		} catch (ClassNotFoundException e) {
			System.out.println(e+ " a class Exception in getRecipesFromFile");
			return null;
		}
	}
	
	/**public void writeRecipesToFile(ArrayList<Recipe> toRecord) {
		File file = new File(getFilesDir(), "recipe_list");
		try {
			if (file.exists()) {
				file.delete();
			}
			file.createNewFile();
			ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream(file));
			out.writeObject(toRecord);
			out.close();
		} catch (IOException e) {
			System.out.println(e + " an IOException in writeRecipesToFile");
			e.printStackTrace();
		}
	}*/
	
	public void writeRecipesToFile(ArrayList<Recipe> toRecord) {
		File file = new File(getFilesDir(), "recipe_list");
		try {
			if (file.exists()) {
				file.delete();
			}
			file.createNewFile();
			ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream(file));
			out.writeObject(toRecord);
			out.close();
		} catch (IOException e) {
			System.out.println(e + " an IOException in writeRecipes to File");
			e.printStackTrace();
		}
	}
	
	
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cookbook);
		
		recipes = getRecipesFromFile();
		
		Button button = (Button) findViewById(R.id.add_recipe);
        button.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) 
        	{
        		Intent i = new Intent(getApplicationContext(), NewRecipe.class);
        		startActivity(i);
        	}
        });
		ArrayAdapter<Object> adapter;
        if (recipes == null) recipes = new ArrayList<Recipe>();
        adapter = new ArrayAdapter<Object>(this, R.layout.recipe_header, recipes.toArray());
		ListView lv = this.getListView();
			//((ListView) findViewById(android.R.id.list));
		lv.setAdapter(adapter);
		//lv.setTextFilterEnabled(true);

		lv.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView<?> parent, View view, int position, long id){
				Intent i = new Intent(getApplicationContext(), RecipeView.class);
				i.putExtra("recipe", (Recipe)(parent.getItemAtPosition(position)));
				startActivity(i);
			}
		});
		
		
		//RelativeLayout outer = (RelativeLayout) findViewById(R.id.outer_recipe_list_bear_layout);
		//outer.addView(button, 0);
		
		TextView makeNew = (TextView) findViewById(android.R.id.empty);
		makeNew.setOnClickListener(new OnClickListener() { //the textview saying you have no elements
			@Override
			public void onClick(View v) {
				Intent i = new Intent(getApplicationContext(), NewRecipe.class);
				startActivity(i);	
			}
		});
		makeNew.setOnLongClickListener(new OnLongClickListener() {
			public boolean onLongClick(View v) {
				System.out.println("Our List of Recipes is: " + recipes.size());
				return true;
			}
		});
	}
	
	public void onResume(Bundle savedInstanceState) {
		super.onResume();
		
		recipes = getRecipesFromFile();
		ArrayAdapter<Object> adapter;
		if(recipes==null) recipes = new ArrayList<Recipe>();
		adapter = new ArrayAdapter<Object>(this, R.layout.recipe_header, recipes.toArray());
		ListView lv = this.getListView();
		//((ListView) findViewById(android.R.id.list));
	lv.setAdapter(adapter);
	//lv.setTextFilterEnabled(true);

	lv.setOnItemClickListener(new OnItemClickListener(){
		public void onItemClick(AdapterView<?> parent, View view, int position, long id){
			Intent i = new Intent(getApplicationContext(), RecipeView.class);
			i.putExtra("recipe", (Recipe)(parent.getItemAtPosition(position)));
			startActivity(i);
		}
	});
	TextView makeNew = (TextView) findViewById(android.R.id.empty);
	makeNew.setOnClickListener(new OnClickListener() { //the textview saying you have no elements
		@Override
		public void onClick(View v) {
			Intent i = new Intent(getApplicationContext(), NewRecipe.class);
			startActivity(i);	
		}
	});
	makeNew.setOnLongClickListener(new OnLongClickListener() {
		public boolean onLongClick(View v) {
			System.out.println("Our List of Recipes is: " + recipes.size());
			return true;
		}
	});
	}
}